//
//  utils.hpp
//  in-app-audio-recording
//
//  Created by ken on 2021/12/3.
//

#ifndef utils_h
#define utils_h

void check_int(int ret,const char* msg){
    if(ret!=0){
        perror(msg);
        char msg[5];
        ((int*)msg)[0]=htonl(ret);
        msg[4]=0;
        printf("Return code converted into ASCII message is %s\n",msg);
        exit(1);
    }
}
void check_bool(bool ret,const char* msg){
    if(ret==false){
        perror(msg);
        exit(1);
    }
}
struct sockaddr_in get_sendaddr(){
    int ret;
    struct ifaddrs *ifaddr;
    struct sockaddr_in *r=new struct sockaddr_in;
    ret=getifaddrs(&ifaddr);
    check_int(ret,"Failed to get ifaddrs");
    for(;ifaddr!=NULL;ifaddr=ifaddr->ifa_next){
        struct sockaddr_in *current_addr=(struct sockaddr_in*)ifaddr->ifa_addr;
        int first_segment=current_addr->sin_addr.s_addr>>24;
        if(strcmp(ifaddr->ifa_name,"en0")==0&&first_segment!=6&&first_segment!=0){
            memcpy(r, current_addr, sizeof(struct sockaddr_in));
            r->sin_addr.s_addr|=0xFF000000;
            r->sin_len=0;
            r->sin_family=AF_INET;
            r->sin_port=htons(PORT);
            memset(r->sin_zero,0,sizeof(r->sin_zero));
        }
    }
    freeifaddrs(ifaddr);
    return *r;
}
enum channel_type{
    music=1,
    vocal=2,
};



#endif /* utils_h */
